
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
File cnc;

const int chipSelect = 10;    

int l1=0;
int l2=1;
int cutspeed=2500;

int x = 0;  
int y = 0;
int z = 0;
float dx=0.0;
float dy=0.0;


float fx1=0.0;
float fy1=0.0;
float fx2=0.0;
float fy2=0.0;

int x1=0;
int y1=0;
int x2=0;
int y2=0;

int bx1=0;
int by1=0;
int bx2=0;
int by2=0;

char* menu1[]={"1) Ajustage     ","2) Choix fichier","3) Decoupe      ","4) Init carte SD","5) Vitesse coupe"};

int maxmenu1=5;

char filename[12]="cnc10.txt";
int posfile=10;
int maxposfile=40;

void setup()
{
  pinMode(2, OUTPUT);     
  pinMode(3, OUTPUT);     
  pinMode(4, OUTPUT);     
  pinMode(5, OUTPUT);     
  pinMode(6, OUTPUT);     
  pinMode(7, OUTPUT);     
  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);     
  
  
  Serial.begin(115200);
  pinMode(10, OUTPUT);     
  
  pinMode(18, OUTPUT);  
  digitalWrite(18, HIGH);
  delay(500);
  affmenu();
}

void affmenu(void) {
  Serial.write(254);
  Serial.write(1);    
  Serial.write(menu1[l1]);
  Serial.write(menu1[l2]);  
}

// *************************** AJUSTAGE DES AXES ********************
void ajustage(void) {
    z=0;
    float showdx=0;
    float showdy=0;
    float lshowdx=0;
    float lshowdy=0;
    char* smenu1[]={"+ Axe 1         ","+ Axe 1 & 2     ","+ Axe 2         "};
    int sm1=0;
        Serial.write(254);
        Serial.write(1);        
    
    Serial.write("Ajustage        ");
    Serial.write(smenu1[0]);
   digitalWrite(18, LOW);   
    while (z<50) {
      x = analogRead(14);
      y = analogRead(15);
      z = analogRead(16);
      dx=map(x,0,1023,-100,100);
      dy=map(y,0,1023,-100,100);
      z=map(z,0,1023,-100,100);
      if (z<-50) {
         showdx=0;
         showdy=0;
        sm1=sm1+1;
        if (sm1==3) sm1=0;
        Serial.write(254);
        Serial.write(1);        
        Serial.write(smenu1[sm1]);      
        while (z<-20) {
          z = analogRead(16);
          z=map(z,0,1023,-100,100);      
          delay(10);     
        }      
        delay(100);
      }      
      if ((dx>-5) and (dx<5)) dx=0;
      if ((dy>-5) and (dy<5)) dy=0;    
      fx1=fx1+(dx/100.0);
      fy1=fy1+(dy/100.0);
      bx1=x1;
      by1=y1;
      x1=round(fx1);
      y1=round(fy1);
      if (bx1<x1) {
        digitalWrite(2, LOW);
        digitalWrite(6, LOW);
        showdx-=0.05;
      } else if (bx1>x1) {
        digitalWrite(2, HIGH);
        digitalWrite(6, HIGH);
        showdx+=0.05;
      }
      if (by1<y1) {
        digitalWrite(4, HIGH);        
        digitalWrite(8, HIGH);
        showdy+=0.05;
      } else if (by1>y1) {
        digitalWrite(4, LOW);
        digitalWrite(8, LOW);
        showdy-=0.05;
      }    
      delayMicroseconds(50);      
      if (bx1!=x1) {
        if (sm1<2) digitalWrite(3, HIGH);
        if (sm1>0) digitalWrite(7, HIGH);
      }
      if (by1!=y1) {
        if (sm1<2) digitalWrite(5, HIGH);
        if (sm1>0) digitalWrite(9, HIGH);       
      }
      if ((lshowdx!=round(showdx*1.5)/10.0) || (lshowdy!=round(showdy*1.5)/10.0)) {
        lshowdx= round(showdx*1.5)/10.0;
        lshowdy= round(showdy*1.5)/10.0;
        Serial.write(254);
        Serial.write(1);        
        Serial.write(smenu1[sm1]); 
        Serial.print("X");       
        
        Serial.print(lshowdx);
        Serial.print(" Y");
        Serial.print(lshowdy);  
      }
      delayMicroseconds(400);
      digitalWrite(3, LOW);
      digitalWrite(5, LOW);
      digitalWrite(7, LOW);
      digitalWrite(9, LOW);
      digitalWrite(2, LOW);
      digitalWrite(4, LOW);
      digitalWrite(6, LOW);
      digitalWrite(8, LOW);
      
      delayMicroseconds(400);  
    }
  digitalWrite(18, HIGH);    
  affmenu();
}
// ************************ SELECTION DU FICHIER DE DECOUPE ****************************
void selfile(void) {
        Serial.write(254);
        Serial.write(1);        
        Serial.write("Fichier choisi: "); 
        Serial.write(filename);
    z=-50;
    while (z<-20) {
      z = analogRead(16);
      z=map(z,0,1023,-100,100);      
      delay(10);     
    }       
    delay(100);
    while (z>-50) {
      x = analogRead(14);
      z = analogRead(16);
      x=map(x,0,1023,-100,100);
      z=map(z,0,1023,-100,100);
      if (x<-50) {
        posfile++;
        if (posfile==maxposfile+1) posfile=10;
        sprintf( filename, "cnc%i.txt", posfile );
        Serial.write(254);
        Serial.write(1);        
        Serial.write("Fichier choisi: "); 
        Serial.write(filename);
      
        while (x<-20) {
          x = analogRead(14);
          x=map(x,0,1023,-100,100); 
          delay(10);     
        }
        delay(100);
      }
      if (x>50) {
        posfile--;
        if (posfile==9) posfile=maxposfile;
        sprintf( filename, "cnc%i.txt", posfile );
        Serial.write(254);
        Serial.write(1);        
        Serial.write("Fichier choisi: "); 
        Serial.write(filename);
        while (x>20) {
          x = analogRead(14);
          x=map(x,0,1023,-100,100); 
          delay(10);     
        }    
        delay(100);
      }      
    }
    while (z<-20) {
      z = analogRead(16);
      z=map(z,0,1023,-100,100);      
      delay(10);     
    }   
    delay(100);    
  affmenu();

}

// ************************ DECOUPE ****************************
void decoupe(void) {
    byte bb;
    byte bb1;
    byte bb2;
    int i;
      Serial.write(254);
      Serial.write(1);    
    Serial.write("** Decouper :   ");
    Serial.write(filename);
    Serial.write(" OK ? ");
    z=-50;
    while (z<-20) {
      z = analogRead(16);
      z=map(z,0,1023,-100,100);      
      delay(10);     
    }       
    delay(100);
    while ((z>-50) && (z<50)) {
      z = analogRead(16);
      z=map(z,0,1023,-100,100);      
      delay(10);     
    }       
    delay(100);
    if (z<-50) {
      cnc = SD.open(filename);
      if (cnc) {
        digitalWrite(18, LOW);  
      Serial.write(254);
      Serial.write(1);        
        Serial.write("Decoupe en cours");
        Serial.write(filename);
        delay(100);
        while (cnc.available()) {
            bb=cnc.read();
            for (i=0; i<10; i++) {
              bb1=bb;
              bb2=bb1;
              if (bb2>=128) {bb2-=128;}
              if (bb2>=64) {bb2-=64;digitalWrite(8, HIGH);}           
              if (bb2>=32) {bb2-=32;}
              if (bb2>=16) {bb2-=16;digitalWrite(6, HIGH);}           
              if (bb2>=8) {bb2-=8;}
              if (bb2>=4) {bb2-=4;digitalWrite(4, HIGH);}           
              if (bb2>=2) {bb2-=2;}
              if (bb2>=1) {bb2-=1;digitalWrite(2, HIGH);}    
              bb2=bb1;
              delayMicroseconds(100);              
              if (bb2>=128) {bb2-=128;digitalWrite(9, HIGH);}
              if (bb2>=64) {bb2-=64;}           
              if (bb2>=32) {bb2-=32;digitalWrite(7, HIGH);}
              if (bb2>=16) {bb2-=16;}           
              if (bb2>=8) {bb2-=8;digitalWrite(5, HIGH);}
              if (bb2>=4) {bb2-=4;}           
              if (bb2>=2) {bb2-=2;digitalWrite(3, HIGH);}
              if (bb2>=1) {bb2-=1;}  
              
              delayMicroseconds(cutspeed);    
              digitalWrite(3, LOW);
              digitalWrite(5, LOW);
              digitalWrite(7, LOW);
              digitalWrite(9, LOW);
              digitalWrite(2, LOW);
              digitalWrite(4, LOW);
              digitalWrite(6, LOW);
              digitalWrite(8, LOW);
              delayMicroseconds(cutspeed);   
            } 
            z = analogRead(16);
            z=map(z,0,1023,-100,100);                
            if (z>50) {
              Serial.write(254);
              Serial.write(1);        
              Serial.write("!! ANNULATION !!");     
              Serial.write("!! ANNULATION !!");     
              cnc.close();
              digitalWrite(18, HIGH);       
              delay(2000);         
              return;
            }             
        }
        cnc.close();
        digitalWrite(18, HIGH);            
      } else {
      Serial.write(254);
      Serial.write(1);        
        Serial.write("Erreur fichier  ");
        Serial.write(filename);
        Serial.write(" ");    
        
        delay(2000);      
      }
    } else {
      Serial.write(254);
      Serial.write(1);
      Serial.write("Decoupe annulee!");  
      Serial.write(filename);
      delay(2000);         
    }
    delay(100);    
  affmenu();

}

//*************************** Initialisation manuelle de la carte SD ********************
void initsd(void) {
   Serial.write(254);
   Serial.write(1);
    Serial.print("** Init carte SD");
    Serial.print("Etape 1/2       ");   
  if (!SD.begin(chipSelect)) {
    Serial.print("#Erreur         ");
    Serial.print("Etape 1/2       ");
  } else {
    Serial.print("Initialisation  ");
    Serial.print("carte SD ok 1/2 ");
  }
  delay(1000);  
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.print("#Erreur         ");
    Serial.print("Etape 2/2       ");
  } else {
    Serial.print("Initialisation  ");
    Serial.print("carte SD ok 2/2 ");
  }
  delay(1000);
  affmenu();

}

//*************************** Vitesse de découpe ********************
void selspeed(void) {
    z=-50;
    while (z<-20) {
      z = analogRead(16);
      z=map(z,0,1023,-100,100);      
      delay(10);     
    }       
        Serial.write(254);
        Serial.write(1);
        Serial.write("Temporisation : ");        
        Serial.print(cutspeed);
        Serial.write(" mu sec ");
    delay(100);
    
    while (z>-50) {
      x = analogRead(14);
      z = analogRead(16);
      x=map(x,0,1023,-100,100);
      z=map(z,0,1023,-100,100);
      if (x<-50) {
        cutspeed-=500;
        if (cutspeed<1000) cutspeed=1000;        

        Serial.write(254);
        Serial.write(1);
        Serial.write("Temporisation : ");        
        Serial.print(cutspeed);
        Serial.write(" mu sec ");
        while (x<-20) {
          x = analogRead(14);
          x=map(x,0,1023,-100,100); 
          delay(10);     
        }
      }
      if (x>50) {
        cutspeed+=500;
        if (cutspeed>10000) cutspeed=10000;
        Serial.write(254);
        Serial.write(1);
        Serial.write("Temporisation : ");        
        Serial.print(cutspeed);
        Serial.write(" mu sec ");
        while (x>20) {
          x = analogRead(14);
          x=map(x,0,1023,-100,100);      
          delay(10);     
        }      
      }
    }
    while (z<-20) {
      z = analogRead(16);
      z=map(z,0,1023,-100,100);      
      delay(10);     
    }       
    delay(100);    
  affmenu();
    
}

void loop(void) {
    x = analogRead(14);
    y = analogRead(15);
    z = analogRead(16);
    x=map(x,0,1023,-100,100);
    y=map(y,0,1023,-100,100);
    z=map(z,0,1023,-100,100);
    if (x<-50) {
      l1=l1+1;
      l2=l2+1;
      if (l1==maxmenu1) l1=0;
      if (l2==maxmenu1) l2=0;
      affmenu();

      while (x<-20) {
        x = analogRead(14);
        x=map(x,0,1023,-100,100); 
        delay(10);     
      }
    }
    if (x>50) {
      l1=l1-1;
      l2=l2-1;
      if (l1==-1) l1=maxmenu1-1;
      if (l2==-1) l2=maxmenu1-1;
      affmenu();
      
      while (x>20) {
        x = analogRead(14);
        x=map(x,0,1023,-100,100);      
        delay(10);     
      }      
    }
    if ((z<-50) and (l1==0)) ajustage();
    if ((z<-50) and (l1==1)) selfile();
    if ((z<-50) and (l1==2)) decoupe();
    if ((z<-50) and (l1==3)) initsd();    
    if ((z<-50) and (l1==4)) selspeed();        
    delay(1);
}